<template>
  <div class="login">
    <div class="box">
      <h1>在线教育网后台管理系统</h1>
      <el-form :model="form"
               status-icon
               ref="form"
               :rules="rules"
               class="demo-ruleForm">
        <el-form-item prop="email">
          <el-input placeholder="用户名"
                    prefix-icon="iconfont icon-lock"
                    v-model="form.email">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input class="mm"
                    placeholder="密码"
                    type="password"
                    prefix-icon="iconfont icon-user"
                    v-model="form.password">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="login"
                     style="width:100%">登录</el-button>
        </el-form-item>
        
      </el-form>
      <a style="color: #fff" href="/register">没有账号？立即注册!</a>
    </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  data() {
    return {
      form: {
        email: '',
        password: '',
      },
      rules: {
        email: [
          { required: true, message: '请输入用户名', trigger: ['blur','change']},
          { max: 20, min: 5,message:'用户名不正确', trigger: ['blur','change'] },
        ],
        password: [
          { required: true, message: '请输入密码', trigger:  ['blur','change']},
          { max: 20, min: 5, message: '密码不正确', trigger:  ['blur','change']},
        ],
      },
    }
  },
  methods: {
     login() {
     this.$refs.form.validate(async(rules)=>{
      if(!rules){
        return this.$message('用户信息有误')
      }
       var {data:res} = await this.$http({
        url: '/v1/vue3system/api/users/login',
        method: 'post',
        data: {
          email: this.form.email,
          password: this.form.password,
        },
      }).catch(err=>err)
      console.log(res)
      var token = res.token
      localStorage.setItem('token', token)
      this.$router.push({ name: 'home' })
     })
      
    },
    register(){
      this.$router.push('/register')
      console.log(111);
    }
  },
}
</script>
<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;background: #2d3a4b;
  
  // position: relative;
  .box {
    width: 500px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding-top: 30px;
    text-align: center;
    h1 {
      font-size: 24px;
      color: #fff;
      margin-bottom: 30px;
    }
    .el-input {
      border: 1px solid #000;
      background-color: #2d3a4b;
    }
    .el-button {
      margin-top: 10px;
    }
    // .el-input__inner{
    //   background-color: red;
    // }
  }
}
</style>